Option Strict Off
Option Explicit On
Friend Class Terminal
	Inherits System.Windows.Forms.Form
    'Public p_Fnum As Short
    Public a_data(136000) As Byte

	
    'Private Sub cmdDel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDel.Click
    '		If cmdGrid.Row < cmdGrid.Rows And cmdGrid.Row <> 0 Then
    '			cmdGrid.RemoveItem((cmdGrid.Row))
    '		End If
    '	End Sub
    '
    '    Private Sub cmdGo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGo.Click
    'Dim i As Short
    ' Dim inpStr As String
    '     If cmdGrid.RowSel < cmdGrid.Row Then
    '         i = cmdGrid.Row
    '         cmdGrid.Row = cmdGrid.RowSel
    '         cmdGrid.RowSel = i
    '     End If
    '     For i = 0 To cmdGrid.RowSel - cmdGrid.Row
    '         inpStr = cmdGrid.get_TextArray(cmdGrid.RowSel - i)
    '         sndCmd(inpStr)
    '         Sleep((10))
    '     Next
    ' End Sub

    ' Private Sub cmdGrid_dblClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGrid.dblClick
    '   sndCmd((cmdGrid.Text))
    'End Sub

    Private Sub cmdOpenPort_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOpenPort.Click
        On Error Resume Next
        If usb_connect(Val(Baud.Text)) Then
            tmr.Enabled = True
            write_Log("Connected OK! @" & Baud.Text, "USB")
            ' TxRxGrid.AddItem("USB", 0)
            ' TxRxGrid.set_TextMatrix(0, 1, "Connected OK! @" & Baud.Text)
        Else
            tmr.Enabled = False
            write_Log("Failed to Connect", "USB")
            ' TxRxGrid.AddItem("USB", 0)
            ' TxRxGrid.set_TextMatrix(0, 1, "Failed to Connect")
        End If
    End Sub
    Private Sub cmdSend_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSend.Click
        ' Dim inputStr As String
        ' inputStr = cmdGrid.get_TextArray(cmdGrid.RowSel)
        ' sndCmd(inputStr)
    End Sub
    Private Sub cmdUlFile_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdUlFile.Click
        Dim i, d As Long
        Dim fnum As Integer
        fnum = FreeFile()

        upFileOpen.FileName = vbNullString
        'On Error GoTo cancelErr
        'UPGRADE_WARNING: CommonDialog variable was not upgraded Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="671167DC-EA81-475D-B690-7A40C7BF4A23"'
        With upFile
            'UPGRADE_WARNING: The CommonDialog CancelError property is not supported in Visual Basic .NET. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="8B377936-3DF7-4745-AA26-DD00FA5B9BE1"'
            .Title = "Open File to be uploaded"
            'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
            .Filter = "Bin *.bin| *.bin"
            .FilterIndex = 1
            .ShowDialog()
        End With

        Dim sndSize As Short
        Dim fileSize As Double
        Dim cmdSend() As Byte

        sndSize = Val(txtBytes.Text)
        ReDim cmdSend(sndSize - 1)
        Err_message("size: " & sndSize)
        'On Error GoTo endsub
        'Open file for binary and upload the fukr!
        FileOpen(fnum, upFileOpen.FileName, OpenMode.Binary)
        fileSize = LOF(fnum)
        prog.Maximum = fileSize
        'fileSize = 1000
        'Debug.Print "fsize: " & fileSize
        For i = 0 To (fileSize - sndSize) Step sndSize
            sndCmd((txtHead.Text))
            If (fileSize - i) > sndSize Then
                For d = 0 To sndSize - 1
                    'Debug.Print d + i
                    'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
                    FileGet(fnum, cmdSend(d), (i + d + 1))
                Next
                usb_write(cmdSend, sndSize)
            Else
                ReDim cmdSend(fileSize - i - 1)
                For d = 0 To (fileSize - i) - 1
                    'Debug.Print d + i
                    'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
                    FileGet(fnum, cmdSend(d), i + d + 1)
                Next

                usb_write(cmdSend, fileSize - i)
            End If
            write_LogBytes(cmdSend, "Sent:")
            sndCmd((txtFoot.Text))
            Sleep(Val(txtDelay.Text))
            prog.Value = i
        Next
endsub:
        FileClose(fnum)
        Exit Sub
cancelErr:
        If Err.Number = 32755 Then
            Exit Sub
        End If
    End Sub
    Private Sub Terminal_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        'p_Fnum = FreeFile()
        'FileOpen(p_Fnum, My.Application.Info.DirectoryPath & "\log.txt", OpenMode.Append)
        ' cmdGrid.set_ColWidth(0, 3500)
        ' cmdGrid.set_ColAlignment(0, 0)
        ' TxRxGrid.set_ColWidth(1, 5200)
        ' TxRxGrid.set_ColWidth(0, 1000)
        ' TxRxGrid.set_ColAlignment(1, 0)
    End Sub

    Private Sub Terminal_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'FileClose(p_Fnum)
        usb_close()
    End Sub
    Private Sub tmr_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tmr.Tick
        Dim inputStr As String
        Dim inputBytes() As Byte

        Dim bytes As Integer
        If lngHandle <> 0 Then
            inputBytes = usb_read(bytes)
            If bytes > 512 Then
                FT_Purge(lngHandle, FT_PURGE_RX + FT_PURGE_TX)
            End If

            If bytes > 0 And bytes <= 512 Then
                Dim a_byte(bytes - 1) As Byte

                For i As Integer = 1 To bytes
                    a_byte(i - 1) = inputBytes(i - 1)
                Next
                inputStr = cv_HexFromBytes(a_byte)
                'TxRxGrid.AddItem("Recv:", 0)
                write_Log(inputStr, "Recv:")
                'WriteLine(p_Fnum, "Recv: " & inputStr)
            End If
        End If
    End Sub

    ' Private Sub TxRxGrid_dblClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles TxRxGrid.dblClick
    '    TxRxGrid.Clear()
    'End Sub

    Private Sub txtSend_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSend.KeyPress
        Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
        Dim inputStr As String
        inputStr = txtSend.Text
        If KeyAscii = 13 And inputStr <> "" Then
            sndCmd(inputStr)
            '  If cmdGrid.get_TextArray(0) <> txtSend.Text Then
            'cmdGrid.AddItem(inputStr, 0)
            'End If
            'If Len(inputStr) > 32 Then
            '    cmdGrid.RowHeight(0) = 240 * ((Len(inputStr) \ 32) + 1)
            'Else
            'cmdGrid.set_RowHeight(0, 240)
            'End If
            txtSend.Text = vbNullString
        End If
        eventArgs.KeyChar = Chr(KeyAscii)
        If KeyAscii = 0 Then
            eventArgs.Handled = True
        End If
    End Sub
    Private Sub sndCmd(ByRef str_ As String)
        Dim inp() As Byte
        Dim inputStr As String
        If Len(str_) > 0 Then
            'UPGRADE_WARNING: Couldn't resolve default property of object cv_BytesFromHex(). Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            inp = cv_BytesFromHex(str_)
            usb_write(inp, UBound(inp) - LBound(inp) + 1)
            inputStr = cv_HexFromBytes(inp)
            'WriteLine(p_Fnum, "Sent: " & inputStr)
            'TxRxGrid.AddItem("Sent:", 0)
            write_Log(inputStr, "Sent:")
        End If
    End Sub
    Public Sub write_Log(ByVal str_ As String, ByVal comment As String)
        If Len(str_) > 48 Then
            '    TxRxGrid.set_RowHeight(0, 240 * ((Len(str_) \ 48) + 1))
        Else
            '   TxRxGrid.set_RowHeight(0, 240)
        End If
        'TxRxGrid.AddItem(comment, 0)
        'TxRxGrid.set_TextMatrix(0, 1, str_)
        dGrid.RowCount += 1
        dGrid.Item(1, dGrid.RowCount - 1).Value = str_
        dGrid.Item(0, dGrid.RowCount - 1).Value = comment
        dGrid.FirstDisplayedCell = dGrid.Item(0, dGrid.RowCount - 1)
        If Len(str_) >= 30 Then
            dGrid.CurrentRow.Height = 22 * (Len(str_) \ 30)
        End If
        'Dim boo As Integer
        'boo = dGrid.NewRowIndex()
        ' Err_message(boo)

    End Sub
    Public Sub write_LogBytes(ByRef bytes_() As Byte, ByVal comment As String)
        Dim str_ As String
        If UBound(bytes_) - LBound(bytes_) > 1 Then
            str_ = cv_HexFromBytes(bytes_) & " count: " & UBound(bytes_) - LBound(bytes_)
            If Len(str_) > 48 Then
                ' TxRxGrid.set_RowHeight(0, 240 * ((Len(str_) \ 48) + 1))
            Else
                ' TxRxGrid.set_RowHeight(0, 240)
            End If
            dGrid.RowCount += 1
            dGrid.Item(1, dGrid.RowCount - 1).Value = str_
            dGrid.Item(0, dGrid.RowCount - 1).Value = comment
            dGrid.FirstDisplayedCell = dGrid.Item(0, dGrid.RowCount - 1)
            'TxRxGrid.AddItem(comment, 0)
            'TxRxGrid.set_TextMatrix(0, 1, str_)
        End If

    End Sub
    Private Sub UploadFirmware_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadFirmware.Click
        Dim fnum As Integer = FreeFile()
        Dim i As Long
        Try
            CodeFile.Title = "Select Firmware to Upload"
            CodeFile.Filter = "*.bin binary|*.bin"
            CodeFile.ShowDialog()
            'Try
            For i = 0 To 132000
                a_data(i) = &HFF
            Next
            'Debug.Print(a_data(50000))
            FileOpen(fnum, CodeFile.FileName, OpenMode.Binary)
            For i = 1 To LOF(fnum)
                FileGet(fnum, a_data(i - 1), i)
            Next

            Dim f_verify As Boolean
            Dim f_program As Boolean
            If cbVerify.CheckState = CheckState.Checked Then
                f_verify = True
            Else
                f_verify = False
            End If
            If cbProgram.CheckState = CheckState.Checked Then
                f_program = True
            Else
                f_program = False
            End If
            setProgram(a_data, f_verify, f_program, LOF(fnum))
            FileClose(fnum)
        Catch
            write_Log(Err.Description, "error")
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd() As Byte
        Dim txSnd As String
        txSnd = txSend.Text
        If txSnd <> vbNullString Then
            cmd = cv_BytesFromHex(txSnd)
            SPI_out(cmd(0))
            'TxRxGrid.AddItem("Send:", 0)
            write_Log(cv_HexFromBytes(cmd), "Send:")
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cmd(0) As Byte
        cmd(0) = SPI_in()
        'TxRxGrid.AddItem("Recv:", 0)
        txRcv.Text = cv_HexFromBytes(cmd)
        write_Log(cv_HexFromBytes(cmd), "Recv:")
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        tmr.Enabled = False
        SPI_Connect()
        'TxRxGrid.AddItem("SPI:", 0)
        write_Log("SPI Connected", "SPI:")
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        SPI_disconnect()
        'TxRxGrid.AddItem("SPI:", 0)
        write_Log("SPI Disconnected", "SPI:")
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        spi_reset()
        ' TxRxGrid.AddItem("SPI:", 0)
        write_Log("SPI Reset", "SPI:")
    End Sub
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        SPI_Monitor()
    End Sub

    Private Sub chRTS_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chRTS.CheckedChanged
        If lngHandle <> 0 Then
            If sender.checked = False Then
                FT_SetRts(lngHandle)
            Else
                FT_ClrRts(lngHandle)
            End If
        End If
    End Sub
    Private Sub chDTR_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chDTR.CheckedChanged
        If lngHandle <> 0 Then
            If sender.checked = False Then
                FT_SetDtr(lngHandle)
            Else
                FT_ClrDtr(lngHandle)
            End If
        End If
    End Sub
    Private Sub bootReset()
        FT_SetRts(lngHandle)
        Threading.Thread.Sleep(20)
        FT_SetDtr(lngHandle)
        Threading.Thread.Sleep(20)
        FT_ClrDtr(lngHandle)
        Threading.Thread.Sleep(20)
    End Sub
    Private Sub bootLoad()
        Threading.Thread.Sleep(20)
        FT_ClrRts(lngHandle)
        Threading.Thread.Sleep(20)
        FT_SetDtr(lngHandle)
        Threading.Thread.Sleep(20)
        FT_ClrDtr(lngHandle)
        Threading.Thread.Sleep(20)
    End Sub
    Private Sub UploadviaBootloaderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadviaBootloaderToolStripMenuItem.Click
        Dim f_packetSize As Integer = Val(txPacket.Text)
        Dim f_count As Integer
        Dim f_retBytes(0 To 20) As Byte
        Dim f_pageData(0 To f_packetSize + 10) As Byte
        Dim f_page(0 To 1) As Byte
        Dim s_crc As Integer
        Dim f_error As Integer
        Dim f_Crc As Integer
        If chCrc.Checked = True Then f_Crc = f_packetSize + 1 Else f_Crc = f_packetSize
        Dim f_returnBytes(0 To 20) As Byte

        Try
            upFileOpen.ShowDialog()

            If usb_connect(500000) Then
                tmr.Enabled = False
                BikiRom_SetTimeoutHigh(True)
                Dim fnum As Integer = FreeFile()
                FileOpen(fnum, upFileOpen.FileName, OpenMode.Binary)
                Dim f_data(0 To LOF(fnum)) As Byte
                dataClean(f_data)
                For i As Integer = 1 To LOF(fnum)
                    FileGet(fnum, f_data(i - 1), i)
                Next
                FileClose(fnum)
                bootLoad()
                Threading.Thread.Sleep(200)
                Dim f_bytes() As Byte = usb_read(f_count)
                'Console.WriteLine("USB connected" & f_bytes(0) & f_bytes(1))


                If f_bytes(0) = 187 And f_bytes(1) = 175 Then
                    For i As Integer = 0 To 495
                        If pageCheck(i, f_data) Then
                            f_page(0) = HIBYTE(i)
                            f_page(1) = LOBYTE(i)
                            If BikiRom_sendCmdd(f_page, f_returnBytes, 2, 1) Then
                                If f_returnBytes(0) = 170 Then
resendPage:
                                    array_copy(f_data, i * 256, f_pageData, 0, 256)
                                    s_crc = 0
                                    For s_i As Integer = 0 To 255
                                        s_crc += f_pageData(s_i)
                                    Next
                                    f_pageData(256) = LOBYTE(s_crc)
                                    Debug.Print(f_pageData(256))
                                    Debug.Print("DATA: " & s_crc & "CRC calc of : " & f_pageData(256))
                                    write_Log("DATA: " & s_crc & "CRC calc of : " & f_pageData(256), "BL Calc")
                                    If BikiRom_sendCmdd(f_pageData, f_returnBytes, f_Crc, 2) Then
                                        If f_returnBytes(0) = 223 And f_returnBytes(1) = 175 Then
                                            Err_message("Page " & i & " Written OK!")
                                            write_Log("Page " & i & " Written OK!", "BL Page")
                                        ElseIf f_returnBytes(0) = 238 And f_returnBytes(1) = 175 Then
                                            f_error += 1
                                            If f_error <= 100 Then GoTo resendPage
                                        Else
                                            Err_message("Error on return bytes: " & f_returnBytes(0) & " " & f_returnBytes(1))
                                            write_Log("Error on return bytes from page # " & i & " : " & f_returnBytes(0) & " " & f_returnBytes(1), "BL error")
                                        End If
                                    Else
                                        Debug.Print("Error Sending Page Data")
                                        write_Log("Error Sending Page Data page # " & i, "BL error")
                                    End If
                                End If
                            End If
                        End If
                    Next
                End If
                bootReset()
            End If
        Catch ex As Exception
            Err_message(Err.Description)
        End Try
    End Sub
    Private Function BikiRom_BootloaderUpload() As Boolean
        Dim f_packetSize As Integer = 256 'Val(txPacket.Text)
        Dim f_count As Integer
        Dim f_retBytes(0 To 20) As Byte
        Dim f_pageData(0 To f_packetSize + 1) As Byte
        Dim f_page(0 To 1) As Byte
        Dim s_error As Integer
        Dim s_crc As Integer
        Dim f_Crc As Integer
        If chCrc.Checked = True Then f_Crc = f_packetSize + 1 Else f_Crc = f_packetSize
        Dim f_returnBytes(0 To 20) As Byte

        Dim f_pageTwo(0 To f_packetSize + 1) As Byte
        Dim f_pageThree(0 To f_packetSize + 2) As Byte
        Try
            upFileOpen.ShowDialog()
            If usb_connect(500000) Then
                tmr.Enabled = False
                BikiRom_SetTimeoutHigh(True)
                Dim fnum As Integer = FreeFile()
                FileOpen(fnum, upFileOpen.FileName, OpenMode.Binary)
                Dim f_data(0 To LOF(fnum) - 1) As Byte
                dataClean(f_data)
                For i As Integer = 1 To LOF(fnum)
                    FileGet(fnum, f_data(i - 1), i)
                Next
                FileClose(fnum)
                bootLoad()
                Threading.Thread.Sleep(200)
                Dim f_bytes() As Byte = usb_read(f_count)

                f_pageData(0) = 223
                f_pageTwo(0) = 223
                f_pageThree(0) = 223
                Dim f_firstByte() As Byte = {223, f_data(0)}
                Dim f_sendLength As Integer = f_packetSize + 1
                If f_bytes(0) = 187 Then
                    write_Log("Recieved BB OK", "BL ENTER")
                    If BikiRom_sendCmdd(f_firstByte, f_retBytes, 2, 1) Then
                        If f_retBytes(0) = 223 Then
                            write_Log("Recieved OK response from first Seed Byte", "BL START")
                            For i As Integer = 1 To UBound(f_data) Step (f_packetSize * 3)
                                array_copy(f_data, i, f_pageData, 1, f_packetSize)
                                array_copy(f_data, i + f_packetSize, f_pageTwo, 1, f_packetSize)
                                array_copy(f_data, i + (f_packetSize * 2), f_pageThree, 1, f_packetSize)
                                s_crc = 0
                                For d As Integer = 1 To (f_packetSize)
                                    s_crc += CByte(f_pageData(d))
                                Next
                                For d As Integer = 1 To (f_packetSize)
                                    s_crc += CByte(f_pageTwo(d))
                                Next
                                For d As Integer = 1 To (f_packetSize)
                                    s_crc += CByte(f_pageThree(d))
                                Next
                                f_pageThree(f_packetSize + 1) = LOBYTE(s_crc)
                                'write_LogBytes(f_pageData, "Packet1:")
                                'write_LogBytes(f_pageTwo, "Packet2:")
                                'write_LogBytes(f_pageThree, "Packet3:")
reSend:

                                If BikiRom_sendCmdd(f_pageData, f_retBytes, f_packetSize + 1, 1) Then
                                    write_Log("past the first packet", "packet")
                                    If BikiRom_sendCmdd(f_pageTwo, f_retBytes, f_packetSize + 1, 1) Then
                                        write_Log("past the second packet", "packet")
                                        If BikiRom_sendCmdd(f_pageThree, f_retBytes, f_packetSize + 2, 1) = False Then
                                            write_Log("error sending data paccket 3", "ERROR")
                                            Return False
                                            Exit Function
                                        Else
                                            If f_retBytes(0) <> 223 Then
                                                write_Log(Hex(i) & " - Error on return " & Hex(f_retBytes(0)), "error")
                                                s_error = s_error + 1
                                                If s_error >= 3 Then
                                                    Return False
                                                    write_Log("lots of errors", "ERROR")
                                                    Exit Function
                                                End If
                                                GoTo reSend
                                            Else
                                                write_Log("past the third packet", "packet")
                                                Err_message("past the third packet OK!")
                                            End If
                                        End If
                                        ' Exit Function
                                        If (i Mod 1024) = 0 Then
                                            setBar(((i / UBound(f_data)) * 100))
                                        End If
                                    Else
                                        write_Log("Error sending packet 2", "ERROR")
                                    End If
                                Else
                                    write_Log("Error sending packet 1", "ERROR")
                                End If
                            Next
                        Else
                            write_Log("Error from initial seed byte return.. ie no return", "ERROR")
                        End If
                    End If
                    Dim cmd() As Byte = cv_BytesFromHex("FF")
                    If BikiRom_sendCmdd(cmd, f_retBytes, 1, 1) Then
                        setBar((100))
                        Return True
                    Else
                        Return False
                    End If
                End If
                bootReset()
            End If
        Catch ex As Exception
            Err_message(Err.Description)
        End Try
    End Function
    Private Sub BikiRom_SetTimeoutHigh(ByVal bool As Boolean)
        If bool Then
            FT_SetTimeouts(lngHandle, 10000, 0)
        Else
            FT_SetTimeouts(lngHandle, 2000, 0)
        End If
    End Sub

    Private Function pageCheck(ByVal PageNo As Integer, ByVal m_data() As Byte) As Boolean
        Dim check As Boolean = False
        Dim i As Integer
        For i = PageNo * 256 To PageNo * 256 + 256
            If m_data(i) <> &HFFS Then
                check = True
            End If
        Next
        Return check
    End Function

    Public Function arrayLoad(ByRef fileName As String) As Byte()
        On Error GoTo LoadError
        Dim fnum As Integer
        Dim f_array() As Byte = Nothing

        fnum = FreeFile()
        FileOpen(fnum, fileName, OpenMode.Binary)
        If LOF(fnum) <> 0 Then
            ReDim f_array(0 To CInt(LOF(fnum) - 1))
            For i As Integer = 1 To CInt(LOF(fnum))
                FileGet(fnum, f_array(i - 1), i)
            Next
        End If
        FileClose(fnum)
        Return f_array
        Exit Function
LoadError:
        Return Nothing
        FileClose(fnum)
    End Function
    Private Sub dataClean(ByRef m_data() As Byte)
        For i As Integer = 0 To UBound(m_data)
            m_data(i) = 255
        Next
    End Sub
    Private Sub dataSet(ByRef m_data() As Byte)
        For i As Integer = LBound(m_data) To UBound(m_data)
            m_data(i) = 0
        Next
    End Sub
    Public Function LOBYTE(ByVal w As Integer) As Byte
        LOBYTE = w And &HFF
    End Function

    Private Sub UploadviaNEWbootloaderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UploadviaNEWbootloaderToolStripMenuItem.Click
        BikiRom_BootloaderUpload()
    End Sub
End Class